﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemPhieuTamUng : Form
    {
        private string _maPhieuTamUng;

        public ThemPhieuTamUng()
        {
            InitializeComponent();
        }

        public ThemPhieuTamUng(string maPhieuTamUng)
        {
            InitializeComponent();
            _maPhieuTamUng = maPhieuTamUng;
        }

        private void ThemPhieuTamUng_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            // fill combobox
            var listPhieuMuaHang = from qt in dc.QuyTrinhMuaHangs
                                   where qt.deleted == false &&
                                         qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.DANG_MUA_HANG
                                   select qt.PhieuMuaHang;

            foreach (_DB.PhieuMuaHang p in listPhieuMuaHang)
            {
                comboBox1.Items.Add(p);
            }
            
            // if edit, show info
            if (_maPhieuTamUng != null)
            {
                _DB.PhieuTamUng phieu = dc.PhieuTamUngs.Single(x => x.MaPhieu == _maPhieuTamUng);

                txtMaPhieu.Text = phieu.MaPhieu;
                txtMaPhieu.ReadOnly = true;
                comboBox1.Items.Clear();
                comboBox1.Items.Add(phieu.QuyTrinhMuaHangs.Single().PhieuMuaHang);
                comboBox1.SelectedItem = phieu.QuyTrinhMuaHangs.Single().PhieuMuaHang;
                comboBox1.Enabled = false;

                txtTien.Text = phieu.SoTien.ToString();
                dtpThoiGian.Value = phieu.ThoiGian.Value;
                txtNoiDung.Text = phieu.NoiDung;
            }
            else
                txtMaPhieu.Text = _DB.DinhDangMaPhieu.getMaPhieuMoi(_DB.DinhDangMaPhieu.DINH_DANG_PHIEU_TAM_UNG_ID);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            _DB.PhieuMuaHang p = (_DB.PhieuMuaHang) comboBox1.SelectedItem;
            if (p != null)
            {
                OtherForms.XemPhieuMuaHang f = new OtherForms.XemPhieuMuaHang(p.MaPhieu);
                f.Show();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

                // edit
                if (_maPhieuTamUng != null)
                {
                    _DB.PhieuTamUng phieu = dc.PhieuTamUngs.Single(x => x.MaPhieu == _maPhieuTamUng);
                    phieu.SoTien = float.Parse(txtTien.Text);
                    phieu.ThoiGian = dtpThoiGian.Value;
                    phieu.NoiDung = txtNoiDung.Text;
                    dc.SubmitChanges();
                    this.Close();
                    return;
                }

                // new, check phieu mua hang
                _DB.PhieuMuaHang phieuMH = (_DB.PhieuMuaHang)comboBox1.SelectedItem;
                if (phieuMH == null)
                {
                    MessageBox.Show("Hãy chọn phiếu mua hàng");
                    return;
                }

                // Tao phieu tam ung
                _DB.PhieuTamUng p = new _DB.PhieuTamUng();
                p.MaPhieu = txtMaPhieu.Text;
                p.MaNguoiTao = Program.MaNguoiDungHienTai;
                p.NgayTao = DateTime.Now;
                p.SoTien = float.Parse(txtTien.Text);
                p.ThoiGian = dtpThoiGian.Value;
                p.NoiDung = txtNoiDung.Text;

                // save
                dc.PhieuTamUngs.InsertOnSubmit(p);
                _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuMuaHang == phieuMH.MaPhieu);
                qt.MaPhieuTamUng = p.MaPhieu;
                qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_TAM_UNG;
                dc.SubmitChanges();

                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
    }
}
